package com.huajx.tb.adapter;

import com.huajx.core.handler.HandlerAdapter;
import com.huajx.core.handler.ServerHandlerFactory;
import com.huajx.core.util.MessageKit;
import com.huajx.tb.dto.NengLongMessage;
import com.huajx.tb.exception.NengLongTcpException;
import com.huajx.tb.handler.*;
import org.apache.mina.core.session.IoSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author: lisa 叶新华
 * @create: 2019-08-28 17:22
 */

public class NengLongHandlerAdapter extends ServerHandlerFactory {
    protected final Logger logger = LoggerFactory.getLogger(NengLongHandlerAdapter.class);
    private static final String MODIFY_IP_PORT = "04";//修改服务器 IP 地址端口号语法(由翼校通平台发给考勤机端) [04]
    private static final String CALL_ACCOUNT = "05";//上传通话话单操作
    private static final String STDT_SCHOOL_RECS = "08";//上传进出校信息操作
    private static final String KEEP_ALIVE = "10";//心跳包语法(由考勤机发给翼校通平台)
    private static final String ABT_STATUS = "82";
    private static final String TIME_CHECK = "0C";//时间校验语法
    private static final String QUERY_CHECK = "0A";//查询考勤机状态
    private static final String CALL_CHECK = "01"; //学生卡请求通话时长语法
    private static final String ALARM_REPORT = "09"; //告警信息上报时间间隔更新语法(由翼校通平台发给考勤机) [09]
    private static final String ALARM_INFO = "03"; //上传告警信息语法(由考勤机发给翼校通平台) [03]

    @Override
    public HandlerAdapter getHandler(Object message) {
        if (message == null) {
            return null;
        }

        NengLongMessage msg = (NengLongMessage) message;

        String command = msg.getCommand();

        if (MODIFY_IP_PORT.equals(command)) {
            return ModifyIpAndPortHandler.me;
        } else if (STDT_SCHOOL_RECS.equals(command)) {
            return StudentSchoolRecords.me;
        } else if (KEEP_ALIVE.equals(command)) {
            return KeepAliveHandler.me;
        } else if (ABT_STATUS.equals(command)) {
        } else if (QUERY_CHECK.equals(command)) {
//			return QueryStatusHandler.me;
        } else if (TIME_CHECK.equals(command)) {
            return TimeCheckHandler.me;
        } else if (CALL_CHECK.equals(command)) {
            return CallCheckHandler.me;
        } else if (CALL_ACCOUNT.equals(command)) {
            return CallAccountHandler.me;//电话单
        }

        return null;
    }

    @Override
    public void messageReceived(IoSession session, Object message) {
        byte[] bytes = (byte[]) message;

        String serialNo = MessageKit.getStr(bytes, 3, 4); //序号

        String version = MessageKit.getStr(bytes, 7, 1);//版本号

        String drive_id = MessageKit.getStr(bytes, 8, 18);//机器码
        int packLen = 0;
        try {
            packLen = MessageKit.getInt(bytes, 26, 4);//包长度
        } catch (Throwable e) {
            logger.info("取包长异常，包:{}", MessageKit.bytesToString(bytes));
        }

        String command = null;
        try {
            command = MessageKit.getStr(bytes, 30, 2);
        } catch (Throwable e) {
            logger.info("取命令异常，包:{}", MessageKit.bytesToString(bytes));
        }

//		String command = TbDecoderAdapter.bytesToString(Arrays.copyOfRange(bytes, 4, 6));//(char)bytes[5] + "" + (char)bytes[6];

        logger.info("设备ID：{}, 处理命令: {}, 包长: {}", drive_id, command, packLen);

        NengLongMessage dto = new NengLongMessage();

        dto.setBody(bytes);
        dto.setDeviceId(drive_id);
        dto.setSerialNo(serialNo);
        dto.setVersion(version);
        dto.setCommand(command);
        dto.setSync(MessageKit.getStr(bytes, 0, 3));
        dto.setPackageLength(packLen);

        try {
            super.messageReceived(session, dto);
        } catch (NengLongTcpException e) {
            logger.warn("包处理异常{}，包:{}", e.getMessage(), MessageKit.bytesToString(bytes));
        }

    }

}
